import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MovieDAO {

    /**
     * 根据ID查找电影
     */
    public Movie findMovieById(String id) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Movie movie = null;

        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT * FROM movies WHERE id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, id);
            rs = pstmt.executeQuery();

            if (rs.next()) {
                movie = new Movie();
                movie.setId(rs.getString("id"));
                movie.setName(rs.getString("name"));
                movie.setDuration(rs.getString("duration"));
                movie.setType(rs.getString("type"));
                movie.setPrice(rs.getDouble("price"));
            }
        } catch (SQLException e) {
            System.out.println("查找电影失败: " + e.getMessage());
        } finally {
            try {
                if (rs != null) rs.close();
                if (pstmt != null) pstmt.close();
                DBUtil.closeConnection(conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return movie;
    }

    /**
     * 获取所有电影
     */
    public List<Movie> getAllMovies() {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<Movie> movies = new ArrayList<>();

        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT * FROM movies";
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();

            while (rs.next()) {
                Movie movie = new Movie();
                movie.setId(rs.getString("id"));
                movie.setName(rs.getString("name"));
                movie.setDuration(rs.getString("duration"));
                movie.setType(rs.getString("type"));
                movie.setPrice(rs.getDouble("price"));
                movies.add(movie);
            }
        } catch (SQLException e) {
            System.out.println("获取所有电影失败: " + e.getMessage());
        } finally {
            try {
                if (rs != null) rs.close();
                if (pstmt != null) pstmt.close();
                DBUtil.closeConnection(conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return movies;
    }

    /**
     * 添加电影
     */
    public boolean addMovie(Movie movie) {
        Connection conn = null;
        PreparedStatement pstmt = null;

        try {
            conn = DBUtil.getConnection();
            String sql = "INSERT INTO movies (id, name, duration, type, price) VALUES (?, ?, ?, ?, ?)";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, movie.getId());
            pstmt.setString(2, movie.getName());
            pstmt.setString(3, movie.getDuration());
            pstmt.setString(4, movie.getType());
            pstmt.setDouble(5, movie.getPrice());

            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            System.out.println("添加电影失败: " + e.getMessage());
            return false;
        } finally {
            try {
                if (pstmt != null) pstmt.close();
                DBUtil.closeConnection(conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 更新电影信息
     */
    public boolean updateMovie(Movie movie) {
        Connection conn = null;
        PreparedStatement pstmt = null;

        try {
            conn = DBUtil.getConnection();
            String sql = "UPDATE movies SET name = ?, duration = ?, type = ?, price = ? WHERE id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, movie.getName());
            pstmt.setString(2, movie.getDuration());
            pstmt.setString(3, movie.getType());
            pstmt.setDouble(4, movie.getPrice());
            pstmt.setString(5, movie.getId());

            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            System.out.println("更新电影失败: " + e.getMessage());
            return false;
        } finally {
            try {
                if (pstmt != null) pstmt.close();
                DBUtil.closeConnection(conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 删除电影
     */
    public boolean deleteMovie(String id) {
        Connection conn = null;
        PreparedStatement pstmt = null;

        try {
            conn = DBUtil.getConnection();
            String sql = "DELETE FROM movies WHERE id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, id);

            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            System.out.println("删除电影失败: " + e.getMessage());
            return false;
        } finally {
            try {
                if (pstmt != null) pstmt.close();
                DBUtil.closeConnection(conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
